home *** CD-ROM | disk | FTP | other *** search
/ Computer Inter@ctive 17 / Computer Interactive cdrom 17 - gen 99.iso / ZDNETIT / CONTENT / SMTPCEMS.ZIP / GETDOC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-11-15  |  6.9 KB  |  232 lines

  1. /*
  2. **  GETDOC.C [edit EMAIL.H before compiling]
  3. ** 
  4. **  This is an automated email response program. Users
  5. **  send email to your POP3 account with no body and with
  6. **  either "LIST" or "GET document" in the subject field.
  7. **  This program scans your POP3 mailbox for LIST and GET
  8. **  commands in the subject field and responds by replying
  9. **  with the requested document, provided that it exists.
  10. **  The requested email is deleted.
  11. */
  12.  
  13. #include <windows.h>
  14. #include <stdio.h>
  15. #include "see.h"
  16. #include "email.h"
  17.  
  18. #define MAX_EMAIL_BODY_SIZE 1500
  19.  
  20. #define BUFF_SIZE   2048
  21. #define TEMP_SIZE    128
  22. #define MAX_LIST      20
  23. #define MAX_STR       65
  24.  
  25. static char Buffer[BUFF_SIZE];
  26. static char Temp[TEMP_SIZE];
  27. static char Reply[TEMP_SIZE];
  28. static struct
  29.   {short MsgNbr;
  30.    char Address[1+MAX_STR];
  31.    char Document[1+MAX_STR];
  32.   } EmailData[MAX_LIST];
  33. static int EmailCount = 0;
  34.  
  35. void ErrorExit(int Code)
  36. {seeErrorText(Code,(LPSTR)Buffer,512);
  37.  printf("SEE Error %d: %s\n", Code, Buffer);
  38.  seeClose();
  39.  exit(1);
  40. }
  41.  
  42. #define TAB 0x09
  43.  
  44. LPSTR FirstNonBlank(LPSTR Ptr)
  45. {char c;
  46.  while(1)
  47.    {c = *Ptr;
  48.     /* skip white space */
  49.     if((c!=' ')&&(c!=TAB)) return Ptr;
  50.     Ptr++;
  51.    }
  52. }
  53.  
  54. void StripCRLF(LPSTR Ptr)
  55. {char c;
  56.  while(1)
  57.    {c = *Ptr;
  58.     if(c=='\0') return;
  59.     if((c=='\r')||(c=='\n'))
  60.       {*Ptr = '\0';
  61.        return;
  62.       }
  63.     Ptr++;
  64.    }
  65. }
  66.  
  67. int IsLeft(LPSTR Ptr1, LPSTR Ptr2)
  68. {char c1, c2;
  69.  while(1)
  70.    {c1 = *Ptr1++;
  71.     c2 = *Ptr2++;
  72.     if(c2=='\0') return TRUE;
  73.     if((c1>='a')&&(c1<='z')) c1 = c1 - 'a' + 'A';
  74.     if((c2>='a')&&(c2<='z')) c2 = c2 - 'a' + 'A';
  75.     if(c1!=c2) return FALSE; 
  76.    }
  77. }
  78.  
  79. void StrCpy(LPSTR Dst, LPSTR Src)
  80. {char c;
  81.  while(1)
  82.    {c = *Src++;
  83.     if((c=='\r')||(c=='\n')) c = '\0';
  84.     *Dst++ = c;
  85.     if(c=='\0') break;
  86.    }
  87. }
  88.  
  89. void main(void)
  90. {int i, n; 
  91.  int Code;
  92.  int BufSize;
  93.  int NbrMsg;
  94.  int Version; 
  95.  long MsgSize;
  96.  LPSTR Ptr;
  97.  FILE *FilePtr;
  98.  Version = seeStatistics(SEE_GET_VERSION);
  99.  printf("SEE4C Version %1x.%1x.%1x\n",0x0f&(Version>>8),0x0f&(Version>>4),0x0f&Version);
  100.  /* define diagnostics log file */
  101.  seeStringParam(SEE_LOG_FILE, (LPSTR)"getdoc.log"); 
  102.  /* connect to POP3 server */
  103.  printf("Connecting to POP3 server %s\n",POP3_HOST_NAME);
  104.  Code = seePop3Connect(
  105.     (LPSTR)POP3_HOST_NAME,             
  106.     (LPSTR)POP3_USER_NAME,            
  107.     (LPSTR)POP3_PASSWORD);            
  108.  if(Code<0) ErrorExit(Code); 
  109.  /* get # messages waiting  */
  110.  puts("Getting message count...");
  111.  NbrMsg = seeGetEmailCount();                   
  112.  if(NbrMsg<0) ErrorExit(NbrMsg); 
  113.  printf("%d messages waiting.\n", NbrMsg);
  114.  /* read message headers */
  115.  for(i=1;i<=NbrMsg;i++)
  116.    {if(EmailCount==MAX_LIST) 
  117.       {printf("EmailData overflow\n");
  118.        break;
  119.       }
  120.     /* get size of waiting email (counting header lines) */
  121.     MsgSize = seeGetEmailSize(i);
  122.     /* Message size should be "small" since it should have no body */
  123.     if(MsgSize<=MAX_EMAIL_BODY_SIZE) 
  124.       {/* read message i */
  125.        printf("Reading email message %d \n",i);
  126.        /* read header + first 3 lines */
  127.        BufSize = seeGetEmailLines(i, 3, (LPSTR)Buffer, BUFF_SIZE); 
  128.        if(BufSize<0) ErrorExit(Code);
  129.        Buffer[BufSize] = '\0';
  130.        /* look for "FROM: " line */
  131.        n = seeExtractText((LPSTR)Buffer, "From: ", (LPSTR)Reply, TEMP_SIZE);
  132.        if(n>0) 
  133.          {Ptr = (LPSTR)Reply+6;
  134.           StripCRLF(Ptr);
  135.           ///printf("%s\n", (LPSTR)Reply);
  136.           if(strchr(Ptr,'<')==NULL)
  137.             {/* sourround with angle brackets */
  138.              EmailData[EmailCount].Address[0] = '<';
  139.              strcpy(&EmailData[EmailCount].Address[1],Ptr);
  140.              strcat(&EmailData[EmailCount].Address[2],">");
  141.             }
  142.           else strcpy(&EmailData[EmailCount].Address[0],Ptr); 
  143.           /* look for "SUBJECT: " line */
  144.           n = seeExtractText((LPSTR)Buffer, "Subject: ", (LPSTR)Temp, TEMP_SIZE);
  145.           if(n>0)
  146.             {///printf("%s", (LPSTR)Temp);
  147.              Ptr = FirstNonBlank((LPSTR)&Temp[8]);
  148.              if(IsLeft((LPSTR)Ptr,(LPSTR)"LIST"))
  149.                {printf("Adding LIST request.\n");
  150.                 EmailData[EmailCount].MsgNbr = i;
  151.                 StrCpy((LPSTR)EmailData[EmailCount].Document,(LPSTR)"list.txt");
  152.                 EmailCount++;
  153.                }
  154.              else if(IsLeft((LPSTR)Ptr,(LPSTR)"GET "))
  155.                {printf("Adding GET request.\n");
  156.                 Ptr = FirstNonBlank((LPSTR)(Ptr+3));
  157.                 EmailData[EmailCount].MsgNbr = i;
  158.                 StrCpy((LPSTR)EmailData[EmailCount].Document,Ptr);
  159.                 EmailCount++;
  160.                }
  161.              else printf("Cannot recognize subject command [%s]\n", Ptr);
  162.             }
  163.           else printf("Missing Subject:\n");
  164.          }
  165.        else printf("Missing From:\n");
  166.       }
  167.     else printf("Email body is too large.\n");
  168.    } /* end-for */
  169.  
  170.  printf("%d documents requested\n", EmailCount);
  171.  /* display requests */
  172.  for(i=0;i<EmailCount;i++)
  173.    {printf("%2i: Msg=%d From=[%s] Doc=[%s]\n",
  174.         i+1, EmailData[i].MsgNbr, EmailData[i].Address, EmailData[i].Document);
  175.     // Suggestion: log date & time stamped requests to disk.
  176.    }
  177.  
  178.  /* delete email message requests in REVERSE order */
  179.  for(i=EmailCount-1;i>=0;i--)
  180.    {n = EmailData[i].MsgNbr;
  181.     printf("Deleting message # %d\n",n);
  182.     Code = seeDeleteEmail(n);
  183.     if(Code<0) ErrorExit(Code);
  184.    }
  185.  /* close connection to POP3 server */
  186.  seeClose();
  187.  
  188.  /* anything to respond to ? */
  189.  if(EmailCount==0) exit(0);
  190.  
  191.  /* connect to SMTP server */
  192.  printf("Connecting to SMTP server %s\n",SMTP_HOST_NAME);
  193.  Code = seeSmtpConnect(
  194.     (LPSTR)SMTP_HOST_NAME,           
  195.     (LPSTR)YOUR_EMAIL_ADDR,           
  196.     (LPSTR)NULL);     
  197.  if(Code<0) ErrorExit(Code); 
  198.  
  199.  /* send email response*/
  200.  for(i=0;i<EmailCount;i++)
  201.    {printf("Responding to message # %d at %s\n",
  202.        EmailData[i].MsgNbr, (LPSTR)EmailData[i].Address);
  203.     FilePtr = fopen(EmailData[i].Document,"rb");
  204.     if(FilePtr)
  205.       {fclose(FilePtr);
  206.        Code = seeSendEmail(
  207.          (LPSTR)EmailData[i].Address,        
  208.          (LPSTR)NULL,                     
  209.          (LPSTR)NULL,                    
  210.          (LPSTR)"Automated Response",                 
  211.          (LPSTR)"Your requested document is attached.",    
  212.          (LPSTR)EmailData[i].Document);                  
  213.        if(Code<0) ErrorExit(Code); 
  214.       }
  215.     else
  216.       {printf("Cannot find document %s\n",EmailData[i].Document);
  217.        Code = seeSendEmail(
  218.          (LPSTR)EmailData[i].Address,        
  219.          (LPSTR)NULL,                     
  220.          (LPSTR)NULL,                    
  221.          (LPSTR)"Automated Response",                 
  222.          (LPSTR)"Requested document not found.",    
  223.          (LPSTR)NULL); 
  224.        if(Code<0) ErrorExit(Code);   
  225.       }  
  226.    } 
  227.  seeClose();
  228.  printf("Done.\n");
  229. } /* end main */
  230.  
  231.  
  232.